const {queryPhone} = require('../../util/phone');
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors');
const dyMgr = require('../api/dyMgr');
const webSockt = require('../api/webSockt')
const serApi = require("../api/serApi")
const {router} = require("../api/webSockt")
const cRes = {
    code: 0,
    data: '',
    msg: ''
};

const app = express()
//解析我们的form表单提交的数据，Content-Type: application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))
//解析json数据格式
app.use(bodyParser.json())
//设置跨域
app.use(cors())
app.use('/result',router)
//请求超时时间
app.use(function (req, res, next) {
    res.setTimeout(2 * 60 * 1000, function () {
        console.log("Request has timed out.");
        return res.status(408).send("请求超时")
    });
    next();
});

app.get('/', function (req, res) {
    return res.send('抖音服务启动成功！')
})

// 对外提供的接口
app.post('/dy/getUsers', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let urlParam = req.body.urlParam;
        let header = req.body.header;
        let num = req.body.num;
        let taskId = req.body.taskId;
        let orderId = req.body.orderId;
        let code = req.body.code;
        let apiKey = req.body.apiKey;
        result = await dyMgr.getUsers(urlParam, header, num, taskId, orderId, code, apiKey);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/getVideoInfo', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let url = req.body.url;
        result.data = await dyMgr.getVideoInfo(url);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/followUser', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let url = req.body.url;
        let urlParam = req.body.urlParam;
        let header = req.body.header;
        let orderId = req.body.orderId;
        let code = req.body.code;
        let apiKey = req.body.apiKey;
        result.data = await dyMgr.followUser(url, urlParam, header, orderId, code, apiKey);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/diggVideo', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let url = req.body.url;
        let urlParam = req.body.urlParam;
        let header = req.body.header;
        let orderId = req.body.orderId;
        let code = req.body.code;
        let apiKey = req.body.apiKey;
        result.data = await dyMgr.diggVideo(url, urlParam, header, orderId, code, apiKey);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/commentVideo', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let url = req.body.url;
        let urlParam = req.body.urlParam;
        let header = req.body.header;
        let comment = req.body.comment;
        let orderId = req.body.orderId;
        let code = req.body.code;
        let apiKey = req.body.apiKey;
        result.data = await dyMgr.commentVideo(url, urlParam, header, comment, orderId, code, apiKey);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/message', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let phone = req.body.phone.trim();
        let password = req.body.password;
        let device = req.body.deviceId;
        let pList = req.body.pList;
        result = await dyMgr.message(phone, password, device, pList);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/urlMessage', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let phone = req.body.phone.trim();
        let password = req.body.password;
        let device = req.body.deviceId;
        let pList = req.body.pList;
        result = await dyMgr.message(phone, password, device, pList);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})


app.post('/dy/login', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let phone = req.body.phone.trim();
        let password = req.body.password;
        let device = req.body.device;
        result = await dyMgr.login(phone, password, device);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/loginVerify', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let code = req.body.code;
        result = await webSockt.loginVerify(code);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/register', async function (req, res) {
    let result = { ...cRes };
    if (req.body) {
        let regNum = req.body.regNum;
        dyMgr.register(regNum);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

// 内部测试的接口
app.post('/dy/sendCode', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let phone = req.body.phone.trim();
        let phoneInfo = await queryPhone(phone);
        let device = req.body.device;
        console.log('phone = ' + phone + ', phoneInfo = ' + JSON.stringify(phoneInfo));
        result = await webSockt.sendCode(phone, phoneInfo, device);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/smsLogin', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let code = req.body.code;
        result = await webSockt.smsLogin(code);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/smsReg', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let code = req.body.code;
        let password = req.body.password;
        result = await webSockt.smsReg(code, password);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/smsUpdatePassword', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        let code = req.body.code;
        result = await webSockt.smsUpdatePassword(code);
        await serApi.registerLoginRes(true, result.data.phone, "aa123123", result.data.urlParam, result.data.header);
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.post('/dy/exit', async function (req, res) {
    var result = { ...cRes };
    if (req.body) {
        result = await webSockt.exit();
    } else {
        result.code = 1;
        result.message = "解析请求异常";
    }
    return res.json(result);
})

app.listen(3030, () => {
    console.log("抖音服务启动成功！");
    dyMgr.init();
})
